package com.erp.module.jingXiHua

import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.erp.R
import com.erp.bean.JingXiHuaBean
import com.erp.bean.JingXiHuaSouSuoBean
import com.erp.module.jingXiHua.jingXiHuaXiangXi.JingXiHuaActivity
import com.erp.myAdapter.JingXiHuaAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_jingxihua.*

/**
 * Created by Administrator on 2019/4/13/013.
 * 作者：刘洋
 */
class FragmentJingXiHua : BaseKFragment(),JingXiHuaContract.JingXiHuaView, PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener, AdapterView.OnItemClickListener, View.OnClickListener {


    override fun getGongHaoLieBiaoOk(gonghaoid: MutableList<String>, gonghaoname: MutableList<String>) {
        ghid = gonghaoid
        ghname = gonghaoname
    }

    override fun getGongHaoLieBiaoNo(throwble: String) {
    }

    var sousuoBean = JingXiHuaSouSuoBean()
    companion object {
        var bean = JingXiHuaBean()
        var ghid : MutableList<String> = ArrayList()
        var ghname : MutableList<String> = ArrayList()
    }
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        bean = list.get(position)
        val intent = Intent(activity, JingXiHuaActivity::class.java)
        startActivityForResult(intent, 101)
        activity!!.overridePendingTransition(R.anim.start_activity1, R.anim.no_activity)
    }

    lateinit var list : MutableList<JingXiHuaBean>
    lateinit var P: IJingXiHuaP
    var updialog: AlertDialog?=null
    lateinit var adapter : JingXiHuaAdapter
    override fun getLayoutId(): Int {
        return R.layout.fragment_jingxihua
    }

    override fun initData() {
        list = ArrayList()
        P = IJingXiHuaP(this,context!!,list)
        adapter= JingXiHuaAdapter(list, activity!!)
        JingXiHuaLv.adapter=adapter
        JingXiHuaLv.onItemClickListener = this
        Pull.setOnHeaderRefreshListener(this)
        Pull.setOnFooterRefreshListener(this)
        BtGuolv.setOnClickListener(this)
        TvChongzhi.setOnClickListener(this)
        TvStartTime.setOnClickListener(this)
        TvEndTime.setOnClickListener(this)
        Pull.setheader(true)
        Pull.headerRefreshing()
        val listApprovalStatus = ArrayList<String>()
        listApprovalStatus.add("全部")
        listApprovalStatus.add("已完成")
        listApprovalStatus.add("未完成")
        val Approvaladapter = ArrayAdapter(activity!!, R.layout.lv_sp_tvitem, listApprovalStatus)
        SpApprovalStatus.adapter = Approvaladapter
        val listtime = ArrayList<String>()
        listtime.add("上传时间")
        listtime.add("审核时间")
        listtime.add("审批时间")
        val timeadapter = ArrayAdapter(activity!!, R.layout.lv_sp_tvitem, listtime)
        SpTimeCategory.adapter = timeadapter
    }
    fun qingkong(){
        if(!isFirstLoad){
            return
        }
        isFirstLoad = false
        Pull.headerRefreshing()
    }

    override fun onClick(v: View?) {
        when(v){
            BtGuolv -> {
                updialog = Myutil.showupdatedialog(context, "正在过滤", false)
                sousuoBean.duty_name=EtDutyName.text.toString()
                sousuoBean.file_category=EtFileCategory.text.toString()
                sousuoBean.guidance_name=EtGuidanceName.text.toString()
                when(SpTimeCategory.selectedItem.toString()){
                    "上传时间" -> {
                        sousuoBean.time_category = "em.ctime"
                    }
                    "审核时间" -> {
                        sousuoBean.time_category = "em.audit_time"
                    }
                    "审批时间" -> {
                        sousuoBean.time_category = "em.approval_time"
                    }
                }
                sousuoBean.start_time=TvStartTime.text.toString()
                sousuoBean.end_time=TvEndTime.text.toString()
                when(SpApprovalStatus.selectedItem.toString()){
                    "全部" -> {
                        sousuoBean.approval_status = "0"
                    }
                    "已完成" -> {
                        sousuoBean.approval_status = "1"
                    }
                    "未完成" -> {
                        sousuoBean.approval_status = "-1"
                    }
                }
                P.sousuo(sousuoBean)
            }
            TvChongzhi -> {
                updialog = Myutil.showupdatedialog(context, "正在过滤", false)
                EtDutyName.setText("")
                EtFileCategory.setText("")
                EtGuidanceName.setText("")
                SpApprovalStatus.setSelection(0)
                SpTimeCategory.setSelection(0)
                TvStartTime.text=""
                TvEndTime.text=""

                P.xiala()
            }
            TvStartTime ->{
                Dialogutil.showBottoPopupWindow(TvStartTime, "选择开始时间", TvStartTime.text.toString(), activity)
            }
            TvEndTime -> {
                Dialogutil.showBottoPopupWindow(TvEndTime, "选择结束时间", TvEndTime.text.toString(), activity)
            }
        }
    }

    override fun xialaOk() {
        updialog?.dismiss()
        Pull?.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updialog?.dismiss()
        Pull?.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updialog?.dismiss()
        Pull?.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updialog?.dismiss()
        Pull?.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }
}